import Vue from 'vue'
import VueRouter from 'vue-router'

import login from '@/views/login'
import layout from '@/views/layout'
import dashboard from '@/views/dashboard'
import picker from '@/views/dashboard/monthRangePicker'
import entBill from '@/views/entBill'
import staffBill from '@/views/staffBill'
import activityManage from '@/views/welfareManage/activityManage'
import approval from '@/views/welfareManage/approval'
import staffManage from '@/views/authManage/staffManage'
import headerManage from '@/views/invoiceManage/headerManage'
import editTableCellDemo from '@/views/editTableCellDemo'
import editTableRowDemo from '@/views/editTableRowDemo'
import test from '@/views/test'

import { getCookie } from '@/utils/cookieUtils'

Vue.use(VueRouter)

const router = new VueRouter({
   routes: [
      {
         path: '/login',
         name: 'login',
         component: login
      },
      {
         path: '/',
         component: layout,
         redirect: '/dashboard',
         children: [
            {
               path: 'dashboard',
               name: 'dashboard',
               component: dashboard
            },
            {
               path: 'picker',
               name: 'picker',
               component: picker
            }
         ]
      },
      {
         path: '/entBill',
         redirect: '/entBill/index',
         component: layout,
         children: [
            {
               path: 'index',
               name: 'entBill',
               component: entBill
            }
         ]
      },
      {
         path: '/staffBill',
         redirect: '/staffBill/index',
         component: layout,
         children: [
            {
               path: 'index',
               name: 'staffBill',
               component: staffBill
            }
         ]
      },
      {
         path: '/welfareManage',
         redirect: '/welfareManage/activityManage',
         component: layout,
         children: [
            {
               path: 'activityManage',
               name: 'activityManage',
               component: activityManage
            },
            {
               path: 'approval',
               name: 'approval',
               component: approval
            }
         ]
      },
      {
         path: '/authManage',
         component: layout,
         children: [
            {
               path: 'staffManage',
               name: 'staffManage',
               component: staffManage
            }
         ]
      },
      {
         path: '/invoiceManage',
         component: layout,
         children: [
            {
               path: 'headerManage',
               name: 'headerManage',
               component: headerManage
            }
         ]
      },
      {
         path: '/demo/table',
         redirect: '/demo/table/editTableRowDemo',
         component: layout,
         children: [
            {
               path: 'editTableRowDemo',
               component: editTableRowDemo
            },
            {
               path: 'editTableCellDemo',
               component: editTableCellDemo
            }
         ]
      },
      {
         path: '/test',
         name: 'test',
         component: test
      }
   ]
})

router.beforeEach((to, from, next) => {
   if (to.path.includes('login')) {
      if (getCookie('token')) {
         next('/')
      } else {
         next()
      }
   } else {
      if (!getCookie('token')) {
         next('/login')
      } else {
         next()
      }
   }
})

export default router
